<template>
  <div>
    <van-button
      type="primary"
      style="position: fixed; left: 0; top: 0; z-index: 999"
      >==finished:{{ finished }}==loading:{{ loading }}</van-button
    >
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="到底了"
        offset="100"
        @load="onLoad"
      >
        <van-cell
          v-for="(item, index) in list"
          :key="index"
          :title="index + '===' + item.goods_name"
        />
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      pagenum: 0,
      pagesize: 10,
      refreshing: false,
    };
  },
  created () { this.getData() },
  methods: {
    onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      // setTimeout(() => {
      //   for (let i = 0; i < 20; i++) {
      //     this.list.push(this.list.length + 1);
      //   }
      this.pagenum += 1
      this.getData()
      // 加载状态结束
      this.loading = false;

      // 数据全部加载完成
      if (this.pagenum > 3) {
        this.finished = true
        //  return
      }
      // }, 3000);
    },
    async getData () {
      const res = await axios({
        url: 'http://vue-shop-api-t.itheima.net/api/private/v1/goods',
        headers: {
          Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE2ODU0MzEyOTgsImV4cCI6MTY4NTUxNzY5OH0.rMLF5nxywTFwqBVP7RWyNJ_qEXoleuetLMbAHDz-NvI'
        },
        params: {
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      })
      console.log(res);
      this.list = [...this.list, ...res.data.data.goods]
    }
  },
};
</script>
<style lang='less'  scoped>
</style>
